<template>
  <div class="login">

    <div class="formContainer">
      <h3>就业管理系统</h3>
      <el-form ref="loginFormRef" :model="loginForm" status-icon :rules="loginRules" label-width="80px"
        class="loginform">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" autocomplete="off" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" type="password" autocomplete="off" />
        </el-form-item>
        <el-form-item label="身份" prop="identity">
          <div>
            <el-radio-group v-model="loginForm.identity">
            <el-radio-button label="学生" value="student" />
            <el-radio-button label="教师" value="teacher" />
          </el-radio-group>
          </div>
        </el-form-item>
        <el-form-item gutter:20px>
          <el-button type="primary" @click="submitForm()">登录</el-button>
          <el-button type="primary" @click="registForm()">注册</el-button>
          <!-- <el-button type="primary" @click="test()">test</el-button> -->

        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'
import { ElMessage } from 'element-plus'
import { useStore } from 'vuex'
const store = useStore()
const loginForm = reactive({
  username: "",
  password: "",
  identity: "学生",
  gender:"",
  introduction:""
})// 表单绑定的响应式对象
const loginFormRef = ref() //表单的引用对象

const loginRules = reactive({
  username: [
    {
      required: true, message: "请输入用户名", trigger: "blur"
    }
  ],
  password: [
    {
      required: true, message: "请输入密码", trigger: "blur"
    }
  ],
  identity: [
    {
      required: true, message: "选择身份", trigger: "blur"
    }
  ]
})
const router = useRouter()
//提交表单函数
const submitForm = () => {
  //1. 校验表单
  loginFormRef.value.validate(async(valid) => {
    if (valid) {
      const res = await axios.post("/user/login",loginForm)
      console.log(res.data)
      if(res.data.code === 0){
        const userInfo = await axios.get(`/user/findByUsername?username=${loginForm.username}`)
        console.log(userInfo);
        localStorage.setItem("token", userInfo.username);
        store.commit("changeUserInfo",userInfo.data.data)
        console.log(store.state.userInfo)
        ElMessage.success("登录成功")
        if(store.state.userInfo.role==="学生"){
          router.push("/prepare/material")
        }
        else if(store.state.userInfo.role==="教师"){
          router.push("/center")
        }
      }else if(res.data.code === 1){
        ElMessage.error(res.data.message)
      }
    }
  })
  
}

const registForm=()=>{
  loginFormRef.value.validate(async(valid) => {
    if (valid) {
      const registForm = {
        username:loginForm.username,
        password:loginForm.password,
        role:loginForm.identity,
        gender:loginForm.gender,
        introduction:loginForm.introduction
      }
      const res = await axios.post("/user/regist",registForm)
      console.log(res)
      if(res.data.code===1){
        ElMessage.error(res.data.message)
      }else if(res.data.code===0){
        ElMessage.success("注册成功，请登录")
      }
    }
  })

}

const test=async()=>{
  const res = await axios.get("/test")
  console.log(res)
}
</script>

<style lang="scss" scoped>
.login {
  background: url("../assets/bg.jpg");
  width: 100%;
  height: 100%;
  position: fixed;
  margin-top: -65px;
  /*上边距*/
  margin-left: -10px;
  /*左边距*/
  background-size: 100% 100%;
}

.formContainer {
  width: 500px;
  height: 300px;
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  background: rgba($color: #000000, $alpha: 0.5);
  color: white;
  text-align: center;
  padding: 20px;

  h3 {
    font-size: 30px;
  }

  .loginform {
    margin-top: 20px;
  }
}

::v-deep .el-form-item__label {
  color: white;
}
</style>
